<template>
  <div>
    <SearchForm ref="search-form" class="g-box-container" @search="getList" />
    <div class="table-container g-box-container">
      <TableList
        ref="TableList"
        :loading="loading"
        :data-source="dataSource1"
        :month="month"
        :total="dataSource2"
      />
    </div>
  </div>
</template>

<script>
import SearchForm from './components/SearchForm.vue';
import TableList from './components/TableList.vue';
import mixinTable from '@/mixins/mixin-table.js';
import { getAnalysisList } from '@/api/analysisMonth.js';
export default {
  components: {
    SearchForm,
    TableList,
  },
  mixins: [mixinTable],
  data() {
    return {
      dataSource: [],
      dataSource1: [],
      dataSource2: [],
      month: '',
    };
  },
  computed: {
    searchForm() {
      return this.$refs['search-form']?.form || {};
    },
  },
  created() {
    this.$nextTick(() => {
      this.getList();
    });
  },
  methods: {
    getSearchForm() {
      const date = this.$refs['search-form']?.form.date;
      return date;
    },
    async getList() {
      const date = this.getSearchForm();
      this.month = date;
      this.loading = true;
      const result = await getAnalysisList(date).finally(
        () => (this.loading = false)
      );
      this.dataSource = result?.data?.data || [];
      if (this.dataSource.length === 0) {
        return;
      }
      this.dataSource1 = this.dataSource.filter((item) => item.type === 1);
      this.dataSource2 = this.dataSource.filter((item) => item.type !== 1);
      this.dataSource2.forEach((item) => {
        if (item.type === 2) {
          item.salesGroupName = '大片销售总计(Kpcs)';
          item.id = 'bigAllTotal';
        } else {
          item.id = 'totalRevenue';
          item.salesGroupName = '营收总计(亿元)';
          item.bpQuantity = ((item.bpQuantity * 1) / 100000000).toFixed(10);
          item.rpQuantity = ((item.rpQuantity * 1) / 100000000).toFixed(10);
          item.m9Quantity = ((item.m9Quantity * 1) / 100000000).toFixed(10);
        }
      });
      // this.dataSource.forEach((item) => {
      //   item.bigBoardBp = item.bpModelCutRate
      //     ? (item.bpQuantity * 1) / (item.bpModelCutRate * 1)
      //     : 0;
      //   item.bigBoardRp = item.rpModelCutRate
      //     ? (item.rpQuantity * 1) / (item.rpModelCutRate * 1)
      //     : 0;
      //   item.bigBoardM9 = item.m9ModelCutRate
      //     ? (item.fcstQuantity * 1) / (item.m9ModelCutRate * 1)
      //     : 0;
      //   item.totalRevenueBp = item.bpPrice
      //     ? item.bpPrice * 1 * item.bpQuantity
      //     : 0;
      //   item.totalRevenuerp = item.rpPrice
      //     ? item.rpPrice * 1 * item.rpQuantity
      //     : 0;
      //   item.totalRevenueM9 = item.m9Price
      //     ? item.m9Price * 1 * item.fcstQuantity
      //     : 0;
      // });
      // this.$refs['TableList']. = date[5] + date[6];
    },
  },
};
</script>

<style lang="less" scoped>
.table-container {
  padding: 10px 20px;
  box-sizing: border-box;
  overflow: auto;
  height: calc(100vh - 160px);
}
</style>
